<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <title>用户权限管理</title>
    <meta name="renderer" content="webkit"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
    <meta name="apple-mobile-web-app-status-bar-style" content="black"/>
    <meta name="apple-mobile-web-app-capable" content="yes"/>
    <meta name="format-detection" content="telephone=no"/>
    <link rel="stylesheet" href="/resource/common/layui/css/layui.css" media="all"/>
    <link rel="stylesheet" href="/resource/css/common.css" media="all"/>
</head>
<style>

    .layui-tree li i {
        color: #0f8277;
    }

    .expa {
        border-right: 6px solid #009688;
        height: 934px;
    }
</style>
<body>
<div class="layui-row">
    <div class="layui-col-md2 expa">
        <!--树-->
        <ul id="moduleTree" style="margin-top: 10px"></ul>
    </div>
    <div class="layui-col-md10">
        <table id="moduleTables" lay-filter="moduleTables"></table>
        <table id="table1" class="layui-table" lay-filter="table1"></table>
    </div>
</div>
<script type="text/html" id="operate">
    <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除角色</a>
    <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="edit">编辑角色</a>
</script>
<script type="text/html" id="permissionOperate">
    <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除权限</a>
</script>

<script type="text/html" id="header">
    <div class="layui-btn-container" style="overflow: hidden">
        <div style="float: right">
            <button class="layui-btn layui-btn-sm" lay-event="addUser">添加用户</button>
        </div>
    </div>
</script>
<script type="text/html" id="headerPermission">
    <div class="layui-btn-container" style="overflow: hidden">
        <div style="float: right">
            <button class="layui-btn layui-btn-sm" lay-event="addPermission">添加权限</button>
        </div>
    </div>
</script>
<script type="text/javascript" src="/resource/common/layui/layui.js"></script>
<script type="text/javascript" th:inline="javascript">
    layui.config({
        base: '/resource/'
    }).extend({
        treetable: 'treetable-lay/treetable'
    })
    layui.use(['table', 'tree', 'layer', 'laydate', 'form', 'treetable'], function () {
        var table = layui.table,
            $ = layui.$,
            layer = layui.layer,
            form = layui.form;
        var treetable = layui.treetable;
        var roleList = [[${roleList}]];
        console.log(roleList)
        var roleId;
        //树
        layui.tree({
            elem: '#moduleTree' //传入元素选择器
            , nodes: [{ //节点
                name: '角色列'
                , children: roleList
            }]
            //点击函数
            , click: function (node) {
                console.log(node) //node即为当前点击的节点数据
                roleId = node.id;
                if (node.name != "角色列") {
                    active.reload();
                    renderTable(roleId);
                }
            }
        });
        //用户列表
        var tableIns = table.render({
            elem: '#moduleTables'
            , url: '/module/user.do' //数据接口
            , method: 'get'
            , toolbar: '#header'
            , page: true //开启分页
            , id: 'tableId'
            , where: {
                roleId: roleId
            }
            , limits: [10, 15, 30]
            , limit: 15
            , cols: [
                [ //表头
                    {'type': 'checkbox', 'fixed': 'left'}
                    , {field: 'index', title: '序号'}
                    , {field: 'userName', title: '学工号'}
                    , {field: 'realName', title: '姓名'}
                    , {field: 'iphone', title: '手机号'}
                    , {field: 'email', title: '电子邮件'}
                    , {field: 'createdDate', title: '创建时间'}
                    , {field: 'status', title: '状态'}
                    , {title: '操作', toolbar: '#operate', 'fixed': 'right', width: 210}
                ]
            ]
        });
        renderTable();
        //监听用户列的头部工具
        table.on('toolbar(moduleTables)', function (obj) {
            if (obj.event == "addUser") {
                //新增用户
                if (roleId == null) {
                    layer.alert("请选择角色后再进行操作！");
                    return false;
                }
                layer.open({
                    type: 2,
                    title: "添加用户",
                    shadeClose: false, //点击遮罩关闭层
                    area: ['800px', '500px'],
                    content: "/role/addUserForRole.do?roleId=" + roleId
                });
            }
        });
        table.on('toolbar(table1)', function (obj) {
            if (obj.event == "addPermission") {
                //新增权限
                if (roleId == null) {
                    layer.alert("请选择角色后再进行操作！");
                    return false;
                }
                layer.open({
                    type: 2,
                    title: "添加权限",
                    shadeClose: false, //点击遮罩关闭层
                    area: ['800px', '500px'],
                    content: "/permission/addPermissionForRole.do?roleId=" + roleId
                });
            }
        });
        //监听用户列工具条
        table.on('tool(moduleTables)', function (obj) {
            var data = obj.data;
            console.log(data);
            if (obj.event == "del") {
                layer.confirm('是否删除?', function (index) {
                    $.ajax({
                        url: "/role/delUser.do",
                        type: "post",
                        data: {_method: "DELETE", "userId": data.id, "roleId": roleId},
                        dataType: "json",
                        success: function (result) {
                            if (!result.status) {
                                layer.msg(result.msg, {time: 2000});
                                return false;
                            }
                            layer.msg("删除成功!", {time: 1000});
                            active.reload();
                        }
                    });
                    layer.close(index);
                })
            } else if (obj.event === "edit") {
                //修改用户
                layer.open({
                    type: 2,
                    title: "修改用户",
                    shadeClose: false, //点击遮罩关闭层
                    area: ['30%', '70%'],
                    content: "/user/editUserHouser.do?id=" + data.id
                });
            }
        });
        table.on('tool(table1)', function (obj) {
            var data = obj.data;
            console.log(data);
            if (obj.event == "del") {
                layer.confirm('是否删除?', function (index) {
                    $.ajax({
                        url: "/permission/delPermission.do",
                        type: "post",
                        data: {_method: "DELETE", "permissionId": data.id, "roleId": roleId},
                        dataType: "json",
                        success: function (result) {
                            if (!result.status) {
                                layer.msg(result.msg, {time: 2000});
                                return false;
                            }
                            layer.msg("删除成功!", {time: 1000});
                            renderTable(roleId);
                        }
                    });
                    layer.close(index);
                })
            }
        });
        active = {
            reload: function () {
                //执行重载
                table.reload('tableId', {
                    url: '/module/user.do'
                    , method: 'get'
                    , page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    , where: {
                        roleId: roleId
                    }
                });
            }
        };
        //定义方法供子页面调用
        var _tools = {
            func1: function (roleId) {
                renderTable(roleId)
            }
        }
        window.tools = _tools;

        //权限树状列表函数
        function renderTable(roleId) {
            treetable.render({
                treeColIndex: 2,          // treetable新增参数
                treeSpid: 0,             // treetable新增参数
                treeIdName: 'id',       // treetable新增参数
                treePidName: 'parentId',     // treetable新增参数
                treeDefaultClose: true,   // treetable新增参数
                treeLinkage: false,        // treetable新增参数
                elem: '#table1',
                toolbar: '#headerPermission',
                url: '/permission/permissionByRoleId.do',
                where: {
                    roleId: roleId
                },
                cols: [[
                    //{type: 'numbers'},
                    {field: 'id', title: '资源ID'},
                    {field: 'permissionName', title: '资源名'},
                    {field: 'permissionType', title: '类型'},
                    {field: 'url', title: '资源路径'},
                    {toolbar: '#permissionOperate', title: '操作'},
                ]]
            });
        }
    });

    /*//监听工具条
    table.on('tool(userTables)', function (obj) {
        var data = obj.data;
        console.log(data);
        if (obj.event === 'details') {
            查看详情
        } else if (obj.event === 'del') {
            //删除
            layer.confirm('是否删除?', function (index) {
                $.ajax({
                    url: "/user/user.do",
                    type: "post",
                    data: {_method: "DELETE", "ids": data.id},
                    dataType: "json",
                    success: function (result) {
                        if (!result.status) {
                            layer.msg(result.msg, {time: 2000});
                            return false;
                        }
                        layer.msg("删除成功!", {time: 1000});
                        active.reload();
                    }
                });
                layer.close(index);
            })
            //启用用户
        } else if (obj.event === 'startUsing') {
            editStatus(data.id, 1)
        } else if (obj.event === "forbidden") {
            editStatus(data.id, 0)
        } else if (obj.event === "edit") {
            console.log("修改")
            //修改用户
            layer.open({
                type: 2,
                title: "修改用户",
                shadeClose: false, //点击遮罩关闭层
                area: ['450px', '400px'],
                content: "/user/editUserHouser.do?id=" + data.id
            });
        }
    });
*/
</script>

</body>
</html>